# Copyright (c) 2011-2013, fortiss GmbH.
# Licensed under the Apache License, Version 2.0.
#
# Use, modification and distribution are subject to the terms specified
# in the accompanying license file LICENSE.txt located at the root directory
# of this software distribution. A copy is available at
# http://chromosome.fortiss.org/.
#
# This file is part of CHROMOSOME.
#
# $Id: XMESystemTest.cmake 4254 2013-07-17 13:13:22Z geisinger $
# 

if (XME_ENABLE_BUILDSYSTEM_SELFTEST)
    # hex2dec()
    hex2dec("0x" __DEC__)
    xme_assert(${__DEC__} EQUAL 0)
    hex2dec("0x0" __DEC__)
    xme_assert(${__DEC__} EQUAL 0)
    hex2dec("0x1" __DEC__)
    xme_assert(${__DEC__} EQUAL 1)
    hex2dec("0x7" __DEC__)
    xme_assert(${__DEC__} EQUAL 7)
    hex2dec("0xF" __DEC__)
    xme_assert(${__DEC__} EQUAL 15)
    hex2dec("0xf" __DEC__)
    xme_assert(${__DEC__} EQUAL 15)
    hex2dec("0xFF" __DEC__)
    xme_assert(${__DEC__} EQUAL 255)
    hex2dec("0xff" __DEC__)
    xme_assert(${__DEC__} EQUAL 255)
    hex2dec("0x100" __DEC__)
    xme_assert(${__DEC__} EQUAL 256)
    hex2dec("0x7FFFFFFF" __DEC__)
    xme_assert(${__DEC__} EQUAL 2147483647)
    hex2dec("0x80000000" __DEC__)
    xme_assert(${__DEC__} EQUAL 2147483648)

    # dec2hex()
    dec2hex(0 __HEX__)
    xme_assert(${__HEX__} STREQUAL "0x0")
    dec2hex(1 __HEX__)
    xme_assert(${__HEX__} STREQUAL "0x1")
    dec2hex(7 __HEX__)
    xme_assert(${__HEX__} STREQUAL "0x7")
    dec2hex(15 __HEX__)
    xme_assert(${__HEX__} STREQUAL "0xF")
    dec2hex(255 __HEX__)
    xme_assert(${__HEX__} STREQUAL "0xFF")
    dec2hex(256 __HEX__)
    xme_assert(${__HEX__} STREQUAL "0x100")
    dec2hex(2147483647 __HEX__)
    xme_assert(${__HEX__} STREQUAL "0x7FFFFFFF") # This is the maximum value that can be represented by CMake, larger values will be changed to 2147483647!
    #dec2hex(2147483648 __HEX__)
    #xme_assert(${__HEX__} STREQUAL "0x80000000")
    
    # xme_simplify_path() with "."
    xme_simplify_path(__PATH__ "/some/./dir")
    xme_assert(${__PATH__} STREQUAL "/some/dir")
    xme_simplify_path(__PATH__ "/some/././dir")
    xme_assert(${__PATH__} STREQUAL "/some/dir")
    xme_simplify_path(__PATH__ "/some/./")
    xme_assert(${__PATH__} STREQUAL "/some")
    xme_simplify_path(__PATH__ "/some/.")
    xme_assert(${__PATH__} STREQUAL "/some")
    xme_simplify_path(__PATH__ "/./some/dir")
    xme_assert(${__PATH__} STREQUAL "/some/dir")
    xme_simplify_path(__PATH__ "/./some/./dir")
    xme_assert(${__PATH__} STREQUAL "/some/dir")

    # xme_simplify_path() with ".."
    xme_simplify_path(__PATH__ "/some/path/..")
    xme_assert(${__PATH__} STREQUAL "/some")
    xme_simplify_path(__PATH__ "/some/path/name/../../dir")
    xme_assert(${__PATH__} STREQUAL "/some/dir")
    xme_simplify_path(__PATH__ "/some/../../dir")
    xme_assert(${__PATH__} STREQUAL "/../dir")
    xme_simplify_path(__PATH__ "some/../../dir")
    xme_assert(${__PATH__} STREQUAL "../dir")
    xme_simplify_path(__PATH__ "C:/some/path/name/../../dir")
    xme_assert(${__PATH__} STREQUAL "C:/some/dir")

    # xme_simplify_path() with "." and ".." combined
    xme_simplify_path(__PATH__ "/./some/./../dir")
    xme_assert(${__PATH__} STREQUAL "/dir")
    xme_simplify_path(__PATH__ "./some/./../dir")
    xme_assert(${__PATH__} STREQUAL "dir")
    
    # xme_simplify_path() with trailing "/"
    xme_simplify_path(__PATH__ "/some/dir/")
    xme_assert(${__PATH__} STREQUAL "/some/dir")
    xme_simplify_path(__PATH__ "C:/some/dir/")
    xme_assert(${__PATH__} STREQUAL "C:/some/dir")
    xme_simplify_path(__PATH__ "C:/some/../dir/")
    xme_assert(${__PATH__} STREQUAL "C:/dir")
    xme_simplify_path(__PATH__ "C:/")
    xme_assert(${__PATH__} STREQUAL "C:/")
    xme_simplify_path(__PATH__ "/")
    xme_assert(${__PATH__} STREQUAL "/")

    # xme_simplify_path() with strange file names
    xme_simplify_path(__PATH__ "dir./some/./../dir")
    xme_assert(${__PATH__} STREQUAL "dir./dir")
    xme_simplify_path(__PATH__ "dir.name/some/./../dir")
    xme_assert(${__PATH__} STREQUAL "dir.name/dir")
    xme_simplify_path(__PATH__ "dir../some/./../dir")
    xme_assert(${__PATH__} STREQUAL "dir../dir")
    xme_simplify_path(__PATH__ "./")
    xme_assert(${__PATH__} STREQUAL ".")
    xme_simplify_path(__PATH__ "")
    xme_assert(${__PATH__} STREQUAL ".")
    #xme_simplify_path(__PATH__ "dir/..some/./../dir")
    #xme_assert(${__PATH__} STREQUAL "dir/dir") # TODO: Currently fails!

    # xme_append() and xme_contains() with variable
    if (TESTVAR)
        message(WARNING "Warning: Self-test uses defined variable. Please adapt self-test.")
    endif ()
    set (TESTVAR)
    
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (VARIABLE "TESTVAR" "1")
    xme_assert(${TESTVAR} STREQUAL "1")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (VARIABLE "TESTVAR" "2")
    string (REPLACE ";" "" TESTVAR_STRING ${TESTVAR})
    xme_assert(${TESTVAR_STRING} STREQUAL "12")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (VARIABLE "TESTVAR" "1")
    string (REPLACE ";" "" TESTVAR_STRING ${TESTVAR})
    xme_assert(${TESTVAR_STRING} STREQUAL "121")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (VARIABLE "TESTVAR" "1" UNIQUE)
    string (REPLACE ";" "" TESTVAR_STRING ${TESTVAR})
    xme_assert(${TESTVAR_STRING} STREQUAL "121")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (VARIABLE "TESTVAR" "3" UNIQUE)
    string (REPLACE ";" "" TESTVAR_STRING ${TESTVAR})
    xme_assert(${TESTVAR_STRING} STREQUAL "1213")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (TESTFLAG)
    
    set (TESTVAR)

    # xme_append() and xme_contains() with global property
    get_property ("TESTPROP_SET" GLOBAL PROPERTY "TESTPROP" SET)
    if (TESTPROP_SET)
        message(WARNING "Warning: Self-test uses defined global property. Please adapt self-test.")
    endif ()
    set_property (GLOBAL PROPERTY TESTPROP)
    
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (PROPERTY_GLOBAL "TESTPROP" "1")
    get_property ("TESTVAR" GLOBAL PROPERTY "TESTPROP")
    xme_assert(${TESTVAR} STREQUAL "1")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (PROPERTY_GLOBAL "TESTPROP" "2")
    get_property ("TESTVAR" GLOBAL PROPERTY "TESTPROP")
    string (REPLACE ";" "" TESTVAR_STRING ${TESTVAR})
    xme_assert(${TESTVAR_STRING} STREQUAL "12")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (PROPERTY_GLOBAL "TESTPROP" "1")
    get_property ("TESTVAR" GLOBAL PROPERTY "TESTPROP")
    string (REPLACE ";" "" TESTVAR_STRING ${TESTVAR})
    xme_assert(${TESTVAR_STRING} STREQUAL "121")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (PROPERTY_GLOBAL "TESTPROP" "1" UNIQUE)
    get_property ("TESTVAR" GLOBAL PROPERTY "TESTPROP")
    string (REPLACE ";" "" TESTVAR_STRING ${TESTVAR})
    xme_assert(${TESTVAR_STRING} STREQUAL "121")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (NOT TESTFLAG)
    
    xme_append (PROPERTY_GLOBAL "TESTPROP" "3" UNIQUE)
    get_property ("TESTVAR" GLOBAL PROPERTY "TESTPROP")
    string (REPLACE ";" "" TESTVAR_STRING ${TESTVAR})
    xme_assert(${TESTVAR_STRING} STREQUAL "1213")
    xme_contains (VARIABLE "TESTVAR" "1" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "2" TESTFLAG)
    xme_assert (TESTFLAG)
    xme_contains (VARIABLE "TESTVAR" "3" TESTFLAG)
    xme_assert (TESTFLAG)
endif (XME_ENABLE_BUILDSYSTEM_SELFTEST)